import os
import base64
from rest_framework.decorators import api_view
from rest_framework.response import Response
from student.models import StudentData,StuSerializer
from django.conf import settings

@api_view(["GET"])
def get_info_by_sfz(request):
    sfzid = request.query_params.get("sfzid")
    stu = StudentData.objects.filter(sfzid=sfzid).first()
    if not stu:
        return Response({
            "result":False,
            "code":400001,
            "message":"找不到学生信息"
        })
    data = StuSerializer(stu).data
    MEDIA = settings.MEDIA_ROOT
    ex_path = data["exam_img"].lstrip("/").lstrip("\\")
    exam_img_path = os.path.join(MEDIA,ex_path)
    with open(exam_img_path, 'rb') as f:
        image_data = f.read()
        base64_data = base64.b64encode(image_data)  # base64编码
        data["exam_img"] = base64_data
    return Response({
        "result":True,
        "code":0,
        "data":data
    })
